from django.shortcuts import render
from django.views import View
from django.http import HttpResponse
from utils.pageTool import get_objects_page
from .models import *

# Create your views here.
#使用视图类的方式去定义视图函数
class TestView(View):
    #通过get方法去响应get请求
    def get(self,request,pagenum=1):
        object_list = Goods.objects.all()
        page,goodses = get_objects_page(object_list,pagenum,5)
        page_range = page.paginator.page_range
        return render(request,'test.html',{'goodses':goodses,'page_range':page_range})


class IndexView(View):
    categorys = Category.objects.all()   #获取所有的商品类别   类属性
    current_category_id = Category.objects.first().id    #当前类别的id号
    def get(self,request,category_id=None,pagenum=1):
        #处理一下类别的id号
        if category_id == None:
            current_cid = self.current_category_id
        else:
            current_cid = category_id
        #调用分页的工具函数
        pages,goods = get_objects_page(Goods.objects.filter(category_id=current_cid),pagenum)
        return render(request,'index.html',{'categorys':self.categorys,'current_cid':int(current_cid),'pages':pages,'goods':goods})